home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJLSR200.ZIP / src / libc / ansi / stdio / flsbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-16  |  1.6 KB  |  95 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <go32.h>
  8. #include <libc/file.h>
  9.  
  10. int
  11. _flsbuf(int c, FILE *f)
  12. {
  13.   char *base;
  14.   int n, rn;
  15.   char c1;
  16.   int size;
  17.  
  18.   if (f->_flag & _IORW)
  19.   {
  20.     f->_flag |= _IOWRT;
  21.     f->_flag &= ~(_IOEOF|_IOREAD);
  22.   }
  23.  
  24.   if ((f->_flag&_IOWRT)==0)
  25.     return EOF;
  26.  
  27.  tryagain:
  28.   if (f->_flag&_IOLBF)
  29.   {
  30.     base = f->_base;
  31.     *f->_ptr++ = c;
  32.     if ((rn = f->_ptr - base) >= f->_bufsiz || c == '\n')
  33.     {
  34.       f->_ptr = base;
  35.       f->_cnt = 0;
  36.     }
  37.     else
  38.     {
  39.       /* we got here because _cnt is wrong, so fix it */
  40.       f->_cnt = -rn;
  41.       rn = n = 0;
  42.     }
  43.   }
  44.   else
  45.     if (f->_flag&_IONBF)
  46.     {
  47.       c1 = c;
  48.       rn = 1;
  49.       base = &c1;
  50.       f->_cnt = 0;
  51.     }
  52.     else
  53.     {
  54.       if ((base=f->_base)==NULL)
  55.       {
  56.     size = _go32_info_block.size_of_transfer_buffer;
  57.     if ((f->_base=base=malloc(size)) == NULL)
  58.     {
  59.       f->_flag |= _IONBF;
  60.       goto tryagain;
  61.     }
  62.     f->_flag |= _IOMYBUF;
  63.     f->_bufsiz = size;
  64.     if (f==stdout && isatty(fileno(stdout)))
  65.     {
  66.       f->_flag |= _IOLBF;
  67.       f->_ptr = base;
  68.       goto tryagain;
  69.     }
  70.     rn = n = 0;
  71.       }
  72.       else
  73.     rn = f->_ptr - base;
  74.       f->_ptr = base;
  75.       f->_cnt = f->_bufsiz;
  76.     }
  77.   while (rn > 0)
  78.   {
  79.     n = write(fileno(f), base, rn);
  80.     if (n <= 0)
  81.     {
  82.       f->_flag |= _IOERR;
  83.       return EOF;
  84.     }
  85.     rn -= n;
  86.     base += n;
  87.   }
  88.   if ((f->_flag&(_IOLBF|_IONBF)) == 0)
  89.   {
  90.     f->_cnt--;
  91.     *f->_ptr++ = c;
  92.   }
  93.   return c;
  94. }
  95.